package com.atguigu.tingshu.order.strategy.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Slf4j
@Service
public class TrackOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient ;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient ;

    @Autowired
    public StringRedisTemplate stringRedisTemplate;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        // 判断
        Long itemId = tradeVo.getItemId();
        Integer trackCount = tradeVo.getTrackCount();
        if(trackCount < 0) {
            throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST) ;
        }

        // 根据当前用户所点击的声音的id，以及购买的声音的数量，查询下单声音列表数据
        Result<List<TrackInfo>> paidTrackInfoList = trackInfoFeignClient.findPaidTrackInfoList(itemId, trackCount);
        List<TrackInfo> trackInfoList = paidTrackInfoList.getData();
        if (CollectionUtils.isEmpty(trackInfoList)) {
            throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST) ;
        }
        Long albumId = trackInfoList.get(0).getAlbumId();

        // 根据专辑的id查询专辑数据
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfoById(albumId);
        AlbumInfo albumInfo = albumInfoResult.getData();

        // 计算原始价格，声音购买没有折扣
        BigDecimal originalAmount = trackCount > 0 ? albumInfo.getPrice().multiply(new BigDecimal(trackCount)) : albumInfo.getPrice() ;

        // 创建一个OrderInfoVo对象
        OrderInfoVo orderInfoVo = new OrderInfoVo() ;
        orderInfoVo.setItemType(SystemConstant.ORDER_ITEM_TYPE_TRACK);
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
        orderInfoVo.setOrderAmount(originalAmount);

        if(!CollectionUtils.isEmpty(trackInfoList)) {
            List<OrderDetailVo> orderDetailVoList = trackInfoList.stream().map(trackInfo -> {
                OrderDetailVo orderDetailVo = new OrderDetailVo();
                orderDetailVo.setItemId(trackInfo.getId());
                orderDetailVo.setItemName(trackInfo.getTrackTitle());
                orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
                orderDetailVo.setItemPrice(albumInfo.getPrice());
                return orderDetailVo;
            }).collect(Collectors.toList());
            orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        }
        orderInfoVo.setOrderDerateVoList(Lists.newArrayList());
        // 给OrderInfoVo设置其他的数据
        this.setOtherDataToOrderInfoVo(orderInfoVo,stringRedisTemplate);
        return orderInfoVo;
    }
    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_TRACK;  // 1002
    }
}